package cn.leetcode;

import java.util.*;

public class Code429 {
    public List<List<Integer>> levelOrder(Node root) {
        List<List<Integer>> result = new ArrayList<>();
        if(root != null){
            LinkedList<Node> linkedList = new LinkedList<>();
            linkedList.add(root);
            while (linkedList.size() != 0){
                int size = linkedList.size();
                List<Integer> list = new ArrayList<>();
                for(int i=0; i<size; i++){
                    Node first = linkedList.removeFirst();
                    list.add(first.val);
                    List<Node> children = first.children;
                    if(children != null){
                        linkedList.addAll(children);
                    }
                }
                result.add(list);
            }
        }
        return result;
    }
}
